package com;

import com.dto.UserDto;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author :Administrator
 * @description: TODO edit class Description
 * @create date: 2021/11/25 15:39
 */
public class ListTest {

    public static void main(String[] args) {

        ArrayList arrayList=new ArrayList();
        ArrayList arrayList1 = null;
//        System.out.println(arrayList.isEmpty()+" " +arrayList1.isEmpty());
        System.out.println(ObjectUtils.isEmpty(arrayList) +" " + ObjectUtils.isEmpty(arrayList1));

//        comparator();
//        remove();
        m2();
    }

    public static void m3(){
        List<String> list1 = Arrays.asList("a", "b", "c", "d", "e");
        List<String> list2 = Arrays.asList("a", "b1", "c2");
    }

    public static void comparator() {
        List<UserDto> userDtos = Arrays.asList(
                UserDto.builder().age(10).name("bbb").build(),
                UserDto.builder().age(22).name("aaa").build(),
                UserDto.builder().age(33).name("bbb").build(),
                UserDto.builder().age(1).name("fff").build(),
                UserDto.builder().age(11).name("fff").build(),
                UserDto.builder().age(13).name("fff").build()
        );
        List<UserDto> collect = userDtos.stream().sorted(Comparator.comparing(UserDto::getName).thenComparing(UserDto::getAge)).collect(Collectors.toList());
        for (UserDto userDto : collect) {
            System.out.println(userDto.getName() + " " + userDto.getAge());
        }
    }

    public static void remove() {
        int creator = new Integer(300);

        List<UserDto> listeners = new ArrayList<>();
        listeners.add(UserDto.builder().age(305).name("bbb").build());
        listeners.add(UserDto.builder().age(300).name("aaa").build());
        listeners.add(UserDto.builder().age(33).name("bbb").build());
        listeners.add(UserDto.builder().age(1).name("fff").build());
        //查询发言人所关注的听众集合
        List<UserDto> speakerFollowUsers = new ArrayList<>();
        speakerFollowUsers.add(UserDto.builder().age(305).name("bbb").build());

        //在发言人所关注的区域，不能重复出现在听众区域
        Iterator it = listeners.listIterator();
        while (it.hasNext()) {
            UserDto userInfo = (UserDto) it.next();
            for (UserDto speakerFollowUser : speakerFollowUsers) {
                if (speakerFollowUser.getAge().intValue()==userInfo.getAge() ) {
                    it.remove();
                    break;
                }
            }
            if (creator==userInfo.getAge()){
                it.remove();
            }
        }
        for (UserDto listener : listeners) {
            System.out.println(listener.getAge() + " " + listener.getName());
        }
    }

    public static void m2() {
        List<UserDto> listeners = new ArrayList<>();
        listeners.add(UserDto.builder().age(10).name("bbb").build());
        listeners.add(UserDto.builder().age(1).name("fff").build());
        listeners.add(UserDto.builder().age(23).name("ddd").build());
        //查询发言人所关注的听众集合
        List<UserDto> speakerFollowUsers = new ArrayList<>();
        speakerFollowUsers.add(UserDto.builder().age(11).name("bbb").build());
        speakerFollowUsers.add(UserDto.builder().age(12).name("aaa").build());
        speakerFollowUsers.add(UserDto.builder().age(16).name("fff").build
                ());
        //aList与bList的交集
        List<UserDto> result =
                listeners.stream().filter(a -> speakerFollowUsers.stream().map(UserDto::getName)
                                        .anyMatch(name -> Objects.equals(a.getName(), name))).collect(Collectors.toList());
        System.out.println("listeners - speakerFollowUsers 交集 :");
        result.parallelStream().forEach(System.out :: println);

//        listeners - speakerFollowUsers 差集
         result =
                listeners.stream().filter(a -> speakerFollowUsers.stream().map(UserDto::getName)
                        .noneMatch(name->Objects.equals(a.getName(), name))).collect(Collectors.toList());
        System.out.println("listeners - speakerFollowUsers 差集 :");
          result.parallelStream().forEach(System.out :: println);

        //        speakerFollowUsers - listeners 差集
        result =
                speakerFollowUsers.stream().filter(a -> listeners.stream().map(UserDto::getName)
                        .noneMatch(name->Objects.equals(a.getName(), name))).collect(Collectors.toList());
        System.out.println(" speakerFollowUsers - listeners 差集 :");
        result.parallelStream().forEach(System.out :: println);

        listeners.addAll(speakerFollowUsers);
        result =listeners.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName()))),
                        ArrayList::new));
        System.out.println(" speakerFollowUsers + listeners 根据指定属性去重 :");
        result.parallelStream().forEach(System.out :: println);
    }
}
